package com.fifth.order.controller;

import com.fifth.common.pojo.dto.CommonResult;
import com.fifth.common.pojo.entity.Cart;
import com.fifth.common.pojo.entity.Order;
import com.fifth.order.service.OrderService;
import com.fifth.order.vo.BusinessOrderVo;
import com.fifth.order.vo.ByNowVo;
import com.fifth.order.vo.GetOrderVo;
import com.fifth.order.vo.PayCartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @作者：刘壬杉
 * @创建时间 2022/9/1 9:33
 **/
@Api(tags = "订单相关接口")
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 点击立即购买按钮(传参:goodsid,addrid,num,price,bid)
     * @param order
     * @return
     */
    @ApiOperation("立即购买按钮的接口")
    @PostMapping("buyNow")
    public CommonResult buyNow(@RequestBody Order order){
        return orderService.buyNow(order);
    }

    /**
     * 在购物车页面点击去结算,修改购物车中被选中商品的状态,并添加到订单中,返回统一的订单编号
     * @param getOrderVos
     * @return 订编单号
     */
    @ApiOperation("(下单)购物车界面点击结算按钮")
    @PostMapping("settlement")
    public CommonResult settlement(@RequestBody List<GetOrderVo> getOrderVos){
        return orderService.settlement(getOrderVos);
    }

    /**
     * 页面加载根据订单号查询订单信息
     * @return
     */
    @ApiOperation(value = "根据订单号查询订单信息")
    @ApiImplicitParam(dataType = "String",name = "code",value = "订单编号",required = true)
    @GetMapping("getOderInfo")
    public CommonResult getOrderInfo( String code){
        return orderService.selectByCode(code);
    }

    /**
     * 支付当前订单生成二维码
     * @param paysort
     * @param code
     * @return
     */

    @ApiOperation(value = "根据订单号和支付类型生成订单二维码")
    @ApiImplicitParams({
        @ApiImplicitParam(dataType = "String",name = "code",value = "订单编号",required = true),
        @ApiImplicitParam(dataType = "String",name = "paysort",value = "支付类型",required = true),
    })
    @PostMapping("payForOrder/{paysort}/{code}")
    public CommonResult paySort(@PathVariable Integer paysort,@PathVariable String code){
        return orderService.paySort(paysort,code);
    }

    /**
     * 根据查询条件分页查询所有订单信息
     * @param pageCurrent
     * @param pageSize
     * @param zhuangtai
     * @return
     */
    @ApiOperation(value = "根据订单状态分页查询订单")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "int",name = "pageCurrent",value = "页码数"),
            @ApiImplicitParam(dataType = "int",name = "pageSize",value = "每页显示个数"),
            @ApiImplicitParam(dataType = "String",name = "zhuangtai",value = "订单状态,''查询所有,0查询已支付,1查询待支付,3查询已失效,4查询已取消,5查询未评价")
    })
    @GetMapping("findOrder/{pageCurrent}/{pageSize}")
    public CommonResult findOrder(@PathVariable Integer pageCurrent,@PathVariable Integer pageSize,@RequestParam(defaultValue = "") String zhuangtai){
        return orderService.findOrder(pageCurrent,pageSize,zhuangtai);
    }

    /**
     * 根据订单id取消订单
     * @param id
     * @return
     */
    @ApiOperation(value = "根据订单id取消订单")
    @ApiImplicitParam(dataType = "String",name = "id",value = "订单id")
    @GetMapping("delOrder/{id}")
    public CommonResult delOrder(@PathVariable String id){
        return orderService.delOrder(id);
    }

    /**
     * 根据code删除订单
     * @param code
     * @return
     */
    @ApiOperation(value = "根据订单编号删除订单(逻辑删除)")
    @ApiImplicitParam(dataType = "String",name = "code",value = "订单编号")
    @GetMapping("deleteOrder/{code}")
    public CommonResult deleteOrder(@PathVariable String code){
        return orderService.deleteOrder(code);
    }

    /**
     * 根据订单编号修改订单状态为已失效(30分钟未支付则失效)
     * @param code
     * @return
     */
    @ApiOperation(value = "根据订单编号修改订单状态为已失效(30分钟未支付则失效)")
    @ApiImplicitParam(dataType = "String",name = "code",value = "订单编号")
    @PostMapping("updateStateToFailure")
    public CommonResult updateStateToFailure(String code){
        return orderService.updateStateToFailure(code);
    }

    /**
     * 结算购物车付款
     * @param payCartVo
     * @return
     */
    @PostMapping("buyCart")
    @ApiOperation("结算购物车付款")
    @ApiImplicitParam(dataType = "PayCartVo",name = "payCartVo",value = "接收购物车结算的实体类")
    public CommonResult buyCart(@RequestBody PayCartVo payCartVo){
        return orderService.buyCart(payCartVo);
    }

    /**
     * 库存不足生成支付二维码失败将购物车商品返还
     * @param getOrderVos
     * @return
     */
    @ApiOperation("库存不足生成支付二维码失败将购物车商品返还")
    @PostMapping("changeCart")
    public CommonResult changeCart(@RequestBody List<GetOrderVo> getOrderVos){
        return orderService.changeCart(getOrderVos);
    }

    /**
     * 商家的订单管理的页面加载
     * @param currentPage
     * @param pageSize
     * @param businessOrderVo
     * @return
     */
    @ApiOperation("商家的订单管理的页面加载")
    @PostMapping("selectAll")
    public CommonResult selectAllOrders(Integer currentPage, Integer pageSize, @RequestBody BusinessOrderVo businessOrderVo){
        return orderService.selectAllOrders(currentPage,pageSize,businessOrderVo);
    }

    /**
     * 根据订单编号修改发货状态
     * @param code
     * @return
     */
    @ApiOperation("根据订单编号修改发货状态")
    @ApiImplicitParam(dataType = "String",name="code",value = "订单编号")
    @PostMapping("updateSend")
    public CommonResult updateSend(String code){
        return orderService.updateSend(code);
    }

    /**
     * 判断该用户是否买过该商品
     * @param id
     * @return
     */
    @PostMapping("hasBuyThisGoods/{id}")
    public CommonResult hasBuyThisGoods(@PathVariable String id){
        return orderService.hasBuyThisGoods(id);
    }
}
